package
{
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import model.CategoryVO;
	import model.ProjectVO;
	import model.TaskVO;
	
	public class ServerConnector
	{

		public var localData:LocalData;
		public var allDataXML:XML = new XML();
		public var allDataURL:String = "http://127.0.0.1:8000/tasks/xml/all/";
		public var allDataRequest:URLRequest = new URLRequest( allDataURL );
		public var xmlLoader:URLLoader = new URLLoader();

		public function ServerConnector(localdata:LocalData)
		{
			this.localData = localdata;
			getData();
		}

		public function getData():void
		{
			//get xml
			xmlLoader.load(allDataRequest);
			xmlLoader.addEventListener(Event.COMPLETE, parseAllData);
		}

		public function parseAllData( event:Event ):void
		{
			allDataXML = XML(xmlLoader.data);
			
			for each ( var task:XML in allDataXML..task )
			{
				trace("TaskDescription " + task.description.toString() );
				trace("project id " + task.project.@id + " name " + task.project.toString());
				var p:ProjectVO = new ProjectVO( task.project.toString(), task.project.@id);
				var c:CategoryVO = new CategoryVO( task.category.toString(), task.category.@id);
				var t:TaskVO = new TaskVO( task.description.toString(),
										   p,
										   c,
										   (task.completed == "True"),
										   new Date( task.due_date.toString().replace(/-/g, "/")),
										   task.@id );
				localData.addTask(t);	   
			}
		}
		
		public function saveProject( project:ProjectVO ):void
		{
			
		}

	}
}